home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / mordor_2.000 / mordor_2 / src / strstr.c < prev    next >
C/C++ Source or Header  |  1994-07-07  |  2KB  |  52 lines

  1. /*Copyright (c) 1991 Xerox Corporation.  All Rights Reserved.
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is
  6.   retained.
  7.  
  8.   A fee may be charged for this program ONLY to recover costs
  9.   for distribution (i.e. media costs).  No profit can be made
  10.   on this program.
  11.  
  12.   The author assumes no responsibility for disasters (natural
  13.   or otherwise) as a consequence of use of this software.
  14.  
  15.   Adam Stein (stein.wbst129@xerox.com)
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. /*This function will find the first occurrence of a midstring (s2) in a
  21.   string (s1).  This function is here because BSD systems don't have it.
  22.  
  23.   Inputs:  s1      - string to search through
  24.        s2      - midstring to search for
  25.   Outputs: pointer - start of midstring in string, NULL if midstring not found
  26.   Locals:  pointer - pointer to go through string
  27.        save_s2 - save s2 since s2 changes when checking
  28.   Globals: NULL    - 0
  29. */
  30. char *strstr(s1,s2)
  31. register char *s1,*s2;
  32. {
  33.     register char *pointer,*save_s2;
  34.  
  35.     save_s2 = s2;
  36.  
  37.     for(pointer = s1;*pointer;++pointer) {
  38.       if(*pointer == *s2)
  39.         while(*pointer && *s2 && *(++pointer) == *(++s2)) ;
  40.  
  41.       /*If we matched every character from s2 then return pointer
  42.         (which is pointing to the end of the substring in the main
  43.         string) - the length of the substring.  Else, return s2 to
  44.         what it was originally and start looking again*/
  45.       if(!(*s2)) return(pointer-strlen(save_s2));
  46.       else s2 = save_s2;
  47.     }
  48.  
  49.     return(NULL);
  50. }
  51.  
  52.